package cn.iocoder.yudao.module.system.controller.admin.userassetscode;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.userassetscode.vo.UserAssetsCodeUsageLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.userassetscode.vo.UserAssetsCodeUsageLogRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.userassetscode.UserAssetsCodeUsageLogDO;
import cn.iocoder.yudao.module.system.service.userassetscode.UserAssetsCodeUsageLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 兑换码使用记录 Controller
 *
 * @author 系统
 */
@Tag(name = "管理后台 - 兑换码使用记录")
@RestController
@RequestMapping("/system/user-assets-code-usage-log")
@Validated
public class UserAssetsCodeUsageLogController {

    @Resource
    private UserAssetsCodeUsageLogService userAssetsCodeUsageLogService;

    @GetMapping("/page")
    @Operation(summary = "获得兑换码使用记录分页")
    @PreAuthorize("@ss.hasPermission('system:user-assets-code:query')")
    public CommonResult<PageResult<UserAssetsCodeUsageLogRespVO>> getUserAssetsCodeUsageLogPage(@Valid UserAssetsCodeUsageLogPageReqVO pageReqVO) {
        PageResult<UserAssetsCodeUsageLogDO> pageResult = userAssetsCodeUsageLogService.getUserAssetsCodeUsageLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserAssetsCodeUsageLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出兑换码使用记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-assets-code:export')")
    public void exportUserAssetsCodeUsageLogExcel(@Valid UserAssetsCodeUsageLogPageReqVO pageReqVO,
                                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAssetsCodeUsageLogDO> list = userAssetsCodeUsageLogService.getUserAssetsCodeUsageLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "兑换码使用记录.xls", "数据", UserAssetsCodeUsageLogRespVO.class,
                BeanUtils.toBean(list, UserAssetsCodeUsageLogRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得兑换码使用记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-assets-code:query')")
    public CommonResult<UserAssetsCodeUsageLogRespVO> getUserAssetsCodeUsageLog(@RequestParam("id") Long id) {
        UserAssetsCodeUsageLogDO usageLog = userAssetsCodeUsageLogService.getUserAssetsCodeUsageLog(id);
        return success(BeanUtils.toBean(usageLog, UserAssetsCodeUsageLogRespVO.class));
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获得用户的兑换码使用记录")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-assets-code:query')")
    public CommonResult<List<UserAssetsCodeUsageLogRespVO>> getUserAssetsCodeUsageLogByUserId(@PathVariable("userId") Long userId) {
        List<UserAssetsCodeUsageLogDO> list = userAssetsCodeUsageLogService.getUserAssetsCodeUsageLogByUserId(userId);
        return success(BeanUtils.toBean(list, UserAssetsCodeUsageLogRespVO.class));
    }
}